@extends('layouts.admin')

@section('title')
    数据库主机 &rarr; 查看 &rarr; {{ $host->name }}
@endsection

@section('content-header')
    <h1>{{ $host->name }}<small>查看此数据库主机的关联数据库和详细信息。</small></h1>
    <ol class="breadcrumb">
        <li><a href="{{ route('admin.index') }}">管理面板</a></li>
        <li><a href="{{ route('admin.databases') }}">数据库主机</a></li>
        <li class="active">{{ $host->name }}</li>
    </ol>
@endsection

@section('content')
    <form action="{{ route('admin.databases.view', $host->id) }}" method="POST">
        <div class="row">
            <div class="col-sm-6">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">主机详情</h3>
                    </div>
                    <div class="box-body">
                        <div class="form-group">
                            <label for="pName" class="form-label">名称</label>
                            <input type="text" id="pName" name="name" class="form-control"
                                value="{{ old('name', $host->name) }}" />
                        </div>
                        <div class="form-group">
                            <label for="pHost" class="form-label">主机地址</label>
                            <input type="text" id="pHost" name="host" class="form-control"
                                value="{{ old('host', $host->host) }}" />
                            <p class="text-muted small">面板连接此MySQL主机创建新数据库时使用的IP地址或域名。</p>
                        </div>
                        <div class="form-group">
                            <label for="pPort" class="form-label">端口</label>
                            <input type="text" id="pPort" name="port" class="form-control"
                                value="{{ old('port', $host->port) }}" />
                            <p class="text-muted small">MySQL在此主机上运行的端口号。</p>
                        </div>
                        <div class="form-group">
                            <label for="pNodeId" class="form-label">关联节点</label>
                            <select name="node_id" id="pNodeId" class="form-control">
                                <option value="">无</option>
                                @foreach($locations as $location)
                                    <optgroup label="{{ $location->short }}">
                                        @foreach($location->nodes as $node)
                                            <option value="{{ $node->id }}" {{ $host->node_id !== $node->id ?: 'selected' }}>
                                                {{ $node->name }}</option>
                                        @endforeach
                                    </optgroup>
                                @endforeach
                            </select>
                            <p class="text-muted small">此设置仅在所选节点上为服务器添加数据库时默认使用此数据库主机。</p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">用户详情</h3>
                    </div>
                    <div class="box-body">
                        <div class="form-group">
                            <label for="pUsername" class="form-label">用户名</label>
                            <input type="text" name="username" id="pUsername" class="form-control"
                                value="{{ old('username', $host->username) }}" />
                            <p class="text-muted small">具有足够权限在系统上创建新用户和数据库的账户用户名。</p>
                        </div>
                        <div class="form-group">
                            <label for="pPassword" class="form-label">密码</label>
                            <input type="password" name="password" id="pPassword" class="form-control" />
                            <p class="text-muted small">上述账户的密码。留空则继续使用当前密码。</p>
                        </div>
                        <hr />
                        <p class="text-danger small text-left">
                            此数据库主机定义的账户<strong>必须</strong>具有<code>WITH GRANT OPTION</code>权限。如果定义的账户没有此权限，创建数据库的请求<em>将会</em>失败。<strong>不要使用与面板相同的MySQL账户凭据。</strong>
                        </p>
                    </div>
                    <div class="box-footer">
                        {!! csrf_field() !!}
                        <button name="_method" value="PATCH" class="btn btn-sm btn-primary pull-right">保存</button>
                        <button name="_method" value="DELETE" class="btn btn-sm btn-danger pull-left muted muted-hover"><i
                                class="fa fa-trash-o"></i></button>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">数据库列表</h3>
                </div>
                <div class="box-body table-responsive no-padding">
                    <table class="table table-hover">
                        <tr>
                            <th>服务器</th>
                            <th>数据库名称</th>
                            <th>用户名</th>
                            <th>连接来源</th>
                            <th>最大连接数</th>
                            <th></th>
                        </tr>
                        @foreach($databases as $database)
                            <tr>
                                <td class="middle"><a
                                        href="{{ route('admin.servers.view', $database->getRelation('server')->id) }}">{{ $database->getRelation('server')->name }}</a>
                                </td>
                                <td class="middle">{{ $database->database }}</td>
                                <td class="middle">{{ $database->username }}</td>
                                <td class="middle">{{ $database->remote }}</td>
                                @if($database->max_connections != null)
                                    <td class="middle">{{ $database->max_connections }}</td>
                                @else
                                    <td class="middle">无限制</td>
                                @endif
                                <td class="text-center">
                                    <a href="{{ route('admin.servers.view.database', $database->getRelation('server')->id) }}">
                                        <button class="btn btn-xs btn-primary">管理</button>
                                    </a>
                                </td>
                            </tr>
                        @endforeach
                    </table>
                </div>
                @if($databases->hasPages())
                    <div class="box-footer with-border">
                        <div class="col-md-12 text-center">{!! $databases->render() !!}</div>
                    </div>
                @endif
            </div>
        </div>
    </div>
@endsection

@section('footer-scripts')
    @parent
    <script>
        $('#pNodeId').select2();
    </script>
@endsection